const { punctuations, specialWords, enWordsMaxLength, enLetterMaxLength, zhMaxLength } = require('../config')
const xlsx = require('node-xlsx').default;
const fs = require("fs");
/**
 * 校验是否是句子,
 * 主要是判断是否有标点符号
 * @param {string} value 
 * @returns 
 */
function isSentence(value) {

    // 如果有特殊词就默认为短语
    if (haveSpecialWord(value)) return false;
    return punctuations.some((punctuation) => {
        return value.includes(punctuation)
    })
}

/**
 * 是否有特殊词
 * @param {string} value 
 * @returns 
 */
function haveSpecialWord(value) {
    // if (endWithSpecialWord(value)) return true;
    return specialWords.some(specialWord => {
        return value.includes(`${specialWord}`)
    });
}

/**
 * 
 * @param {string} value 
 * @returns 
 */
function endWithSpecialWord(value) {
    return specialWords.some(specialWord => value.endsWith(` ${specialWord}`))
}

/**
 * 
 * @param {string} en 
 * @returns 
 */
function isEnBreak(en = '') {
    let enWordLength = en.split(/\s/).length;
    let enLettersLength = en.length;
    return enWordLength > enWordsMaxLength || enLettersLength > enLetterMaxLength;
}

/**
 * 
 * @param {string} zh 
 * @returns 
 */
function isZhBreak(zh = '') {
    let zhLength = zh.length;
    return zhLength > zhMaxLength;
}

/**
 * 校验翻译内容
 * @param {Array<string>} xlsRow 
 * @param {boolean} onlyValidate -决定返回内容
 */
function validateTrans(xlsRow, onlyValidate = false) {
    try {

        const [postion, zh, en, breakReason, diff] = xlsRow;

        xlsRow[3] = '√'
        // 句子
        if (isSentence(en)) {
            if (onlyValidate) return true;
            return xlsRow
        }

        // 短语
        if (isEnBreak(en)) {

            if (onlyValidate) return false

            if (isZhBreak(zh)) {
                xlsRow[3] = '中文词条超长，同时英文翻译超长!\n'
                return xlsRow;
            }
            xlsRow[3] = '英文翻译超长!\n'
            return xlsRow
        }
        if (onlyValidate) return true;
        return xlsRow;
    } catch (error) {
        return xlsRow
    }
}


module.exports = {
    validateTrans,
}